package com.rcd.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.rcd.dao.INewsDao;
import com.rcd.exception.MyException;
import com.rcd.util.Page;
import com.rcd.vo.News;

@Transactional
public class INewsDaoimpl implements INewsDao {

	@Resource private SessionFactory sessionFactory;
	private Query query = null;
	private List<News> list = null;
	private News news = null;
	private String hql = "";
	private int count = 0;
	
	public void addNews(News news) throws MyException {
		try {
			sessionFactory.getCurrentSession().persist(news);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    addNews",
					"-----------INewsDaoimpl" });
		}
	}

	public void delNews(int nid) throws MyException {
		try {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().get(News.class, nid));
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    delNews",
					"-----------INewsDaoimpl" });
		}
	}
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<News> findAll(Page page) throws MyException {
		try {
			hql = "from News n where n.ntitle like ? order by ndate";
			query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, "%" + page.getKeyword() + "%");
			query.setFirstResult((page.getPagecode()*page.getPagesize())-page.getPagesize());
			query.setMaxResults(page.getPagesize());
			list = query.list();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    findAll",
					"-----------INewsDaoimpl" });
		}
		return list;
	}
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public News findById(int nid) throws MyException {
		try {
			news = (News)sessionFactory.getCurrentSession().get(News.class, nid);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    findById",
					"-----------INewsDaoimpl" });
		}
		return news;
	}
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public int getCount(Page page) throws MyException {
		try {
			hql = "from News n where n.ntitle like ? order by ndate";
			query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, "%" + page.getKeyword() + "%");
			count = query.list().size();
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    addNews",
					"-----------INewsDaoimpl" });
		}
		return count;
	}

	public void updateNews(News news) throws MyException {
		try {
			sessionFactory.getCurrentSession().update(news);
		} catch (MyException e) {
			e.printStackTrace(new Object[] { "error   in    addNews",
					"-----------INewsDaoimpl" });
		}
	}

	public void delNewss(Object[] nids) throws MyException {
		// TODO Auto-generated method stub
		
	}
}
